﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
    
namespace Expect.VisualStudio.Extensions
{
    public class SelectionEvents : IVsSelectionEvents
    {
        private static IVsMonitorSelection SelectionService;

        private IExpectPackage _pkg;

        private static uint ContextCookie = 0;

        public SelectionEvents(IVsMonitorSelection selServices, IExpectPackage pkg)
        {
            _pkg = pkg;
            SelectionService = selServices;
            ContextCookie = RegisterContext(selServices);
        }

        private static uint RegisterContext(IVsMonitorSelection selServices)
        {            
            // Initialize the selection service            
            //SelectionService = (IVsMonitorSelection)pkg.GetService(typeof(SVsShellMonitorSelection));

            // Get a cookie for our UI Context. This "registers" our
            // UI context with the selection service so we can set it again later.
            uint retVal;
            Guid uiContext = Guids.guidExpect_Context_ExpectFileSelected;
            SelectionService.GetCmdUIContextCookie(ref uiContext, out retVal);

            return retVal;

        }

        // We don’t care about either of these methods, but it’s useful to know what they do.
        int IVsSelectionEvents.OnCmdUIContextChanged(uint dwCmdUICookie, int fActive)
        {
            return VSConstants.S_OK;
        }

        int IVsSelectionEvents.OnElementValueChanged(uint elementid, object varValueOld, object varValueNew)
        {
            return VSConstants.S_OK;
        }


        int IVsSelectionEvents.OnSelectionChanged(
            IVsHierarchy pHierOld, 
            uint itemidOld,
            IVsMultiItemSelect pMISOld, 
            ISelectionContainer pSCOld,
            IVsHierarchy pHierNew, 
            uint itemidNew,
            IVsMultiItemSelect pMISNew, 
            ISelectionContainer pSCNew)
        {
            
            if (pHierNew != null)
            {
                object fileName;
                object oldfileName = null;

                pHierNew.GetProperty(itemidNew, (int)__VSHPROPID.VSHPROPID_Name, out fileName);
                
                if (pHierOld != null)
                    pHierOld.GetProperty(itemidOld, (int)__VSHPROPID.VSHPROPID_Name, out oldfileName);

                string fname = fileName != null ? fileName.ToString() : "";
                string oldfname = oldfileName != null ? oldfileName.ToString() : "";

                IExpectVSService expectService = (IExpectVSService)_pkg.GetVSService(typeof(SExpectVSService)); // Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SExpectVSService));
                
                if (!string.IsNullOrEmpty(oldfname))
                {
                    expectService.UpdateClassMap(oldfname);
                }

                if (fname.ToLower().EndsWith(Constants.ExpectFileExtension))
                {
                    object objDirectory;
                    pHierNew.GetProperty(itemidNew, (int)__VSHPROPID.VSHPROPID_ProjectDir, out objDirectory);
                    // If we meet the conditions, set the UI context to be active.
                    SelectionService.SetCmdUIContext(ContextCookie, 1);

                    expectService.ExpectFileLoaded(fname);

                    return VSConstants.S_OK;
                }
                else
                {
                    expectService.ClearCurrentItem(fname);
                }
            }
            
            // Otherwise, deactivate it.
            SelectionService.SetCmdUIContext(ContextCookie, 0);
            
            return VSConstants.S_OK;
        }
    }
}